package com.example.springboot.mapper;

import com.example.springboot.bean.LayuiTreeNode;
import com.example.springboot.bean.Menu;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface MenuMapper {

    @Select("select * from hyfc_menu where pid is null")
    // 自定义字段映射  mybatis 强项
    @Results(
            id = "ltn_rm",
            value = {
                    @Result(column = "id", property = "id", id = true),
                    @Result(column = "name", property = "title"),
                    @Result(column = "id", property = "children",
                    many=@Many(select = "selectChildren")),
            }
    )
    List<LayuiTreeNode> selectRootMenu();

    // 根据id查询当前菜单的子菜单
    @Select("select * from hyfc_menu where pid = #{id}")
    @ResultMap("ltn_rm")
    List<LayuiTreeNode> selectChildren(int id);

    // 判断有没有子菜单
    @Select("select count(*) from hyfc_menu where pid = #{id}")
    int countChildren(int id);

    @Delete("delete from hyfc_menu where id=#{id}")
    void deleteById(int id);

    @Update("update hyfc_menu set name=#{name} where id=#{id}")
    void updateName(Menu menu);
}
